﻿using System.Text;
using System.Windows;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        public static void BuildFillBlankExam()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;
            if (Globals.selectedEditor.SelectedExpression is Expressions.BaseText == false)
            {
                MessageBox.Show("只能把文本块转换成填空！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            Expressions.BaseText btx = Globals.selectedEditor.SelectedExpression as Expressions.BaseText;

            ModifingItem mi = new ModifingItem();


            string tmp = btx.Text;

            Action actBackText = new Action(btx.ExID, -1, btx.GetType().Name, "BackText",
                btx.BackText, btx.Text);
            btx.BackText = tmp;
            mi.Add(actBackText);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < (int)btx.Text.Length / 2; i++)
            {
                sb.Append((char)160);
            }

            string newText = sb.ToString() + "▲" + sb.ToString();

            Action actText = new Action(btx.ExID, -1, btx.GetType().Name,
                "Text", btx.Text, newText);
            btx.Text = newText;
            mi.Add(actText);

            if (btx.UnderLine == false)
            {
                Action actUnderLine = new Action(btx.ExID, -1, btx.GetType().Name,
                    "UnderLine", false.ToString(), true.ToString());
                btx.UnderLine = true;
                mi.Add(actUnderLine);
            }

            if (btx.ShowBackText)
            {
                Action actShowBackText = new Action(btx.ExID, -1,
                    btx.GetType().Name, "ShowBackText", true.ToString(), false.ToString());
                btx.ShowBackText = false;
                mi.Add(actShowBackText);
            }

            btx.Editor.Do(mi);
        }
    }
}
